package com.zhangyj.applets;

import com.zhangyj.applets.data.DBService.MenuNode;
import com.zhangyj.applets.data.ParamData;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import com.zhangyj.applets.data.DBService;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class MainApplet extends javax.swing.JApplet {
	private static final long serialVersionUID = -4933341691904801833L;
	MainApplet me;

	public MainApplet() {
		me = this;
	}

	@Override
	public void init() {
		DBService.jsessionid = getParameter("jsessionid");
		DBService.contextpath = getParameter("contextpath");
		try {
			String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
			UIManager.setLookAndFeel(windows);
		} catch (Exception e) {
		}

		initComponents();
	}

	private void initComponents() {
		closableTabbedPane = new com.zhangyj.applets.swing.JClosableTabbedPane(true);
		jsplitUpAndDown = new javax.swing.JSplitPane();
		jsplitUpAndDown.setDividerLocation(50);
		topPanel = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jsplitLeftAndRight = new javax.swing.JSplitPane();
		paneTree = new javax.swing.JScrollPane();
		programTree = new javax.swing.JTree();
		setName("Form");
		getContentPane().setLayout(new java.awt.BorderLayout());

		jsplitUpAndDown.setBorder(null);
		jsplitUpAndDown.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
		jsplitUpAndDown.setName("jSplitPane1");

		topPanel.setName("jPanel1");
		topPanel.setLayout(new java.awt.CardLayout());

		jLabel1.setText("jLabel1.text");
		jLabel1.setName("jLabel1");
		topPanel.add(jLabel1, "card2");

		jsplitUpAndDown.setLeftComponent(topPanel);

		jsplitLeftAndRight.setName("jSplitPane2");
		jsplitLeftAndRight.setDividerLocation(200);
		jsplitLeftAndRight.setRightComponent(closableTabbedPane);

		paneTree.setName("jScrollPane1");

		programTree.setName("jTree1");
		ParamData data = new ParamData();
		data.setActionName("function");
		TreeModel model = DBService.getTreeNode(data);
		programTree.setModel(model);
		programTree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent e) {
				treeValueChanged(e);
			}
		});
		paneTree.setViewportView(programTree);
		jsplitLeftAndRight.setLeftComponent(paneTree);
		jsplitUpAndDown.setRightComponent(jsplitLeftAndRight);
		getContentPane().add(jsplitUpAndDown, java.awt.BorderLayout.CENTER);
	}

	private void treeValueChanged(TreeSelectionEvent e) {
		TreePath path = e.getNewLeadSelectionPath();
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
		MenuNode mnode = (MenuNode) node.getUserObject();
		try {
			if (mnode.sid == -1) {
				return;
			}
			/*
			 * JPanel userform = (JPanel)
			 * Class.forName(mnode.sid).newInstance();
			 * 
			 * panelPage = new FormScrollPane(userform);
			 * panelPage.setName(mnode.sid);
			 * closableTabbedPane.addTab(mnode.description, panelPage);
			 * FormContext context = new FormContext(userform); ((Form)
			 * userform).onload(context);
			 */
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(me, e1.getMessage());
		}
	}

	private javax.swing.JLabel jLabel1;
	private javax.swing.JPanel topPanel;
	private javax.swing.JScrollPane paneTree;
	private javax.swing.JSplitPane jsplitUpAndDown;
	private javax.swing.JSplitPane jsplitLeftAndRight;
	private javax.swing.JTree programTree;
	private com.zhangyj.applets.swing.JClosableTabbedPane closableTabbedPane;
}
